//
//  HWGameDetailTopView.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/10.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import Cosmos
class HWGameDetailTopView: UIView {
    var gameImageView:UIImageView!
    var gameImageTopView:UIImageView!
    var gameImageTopLabel:UILabel!
    var nameLabel:UILabel!
    var sizeLabel:UILabel!
    var lineView:UIView!
    var typeLabel:UILabel!
    var recommondLabel:UILabel!
    var starView:CosmosView!
    var textTagCollectionView:TTGTextTagCollectionView!
    var scoreBgView:UIImageView!
    var scoreLabel:UILabel!
    var score:UILabel!
    var openServiceImage:UIImageView!
    var openServiceTitle:UILabel!
    var openServiceInfo:UILabel!
    var openServiceArrow:UIImageView!
    var openServiceButton:UIButton!
    
    var gameGroupBg:UIImageView!
    var groupNameLabel:UILabel!
    var groupContentLabel:UILabel!
    var joinGroupLabel:UILabel!
    var joinNumLabel:UILabel!
    var joinGroupButton:UIButton!
    
    /// 礼包
    var giftPackageBg:UIView!
    var giftPackageImageView:UIImageView!
    var giftPackageTitleLabel:UILabel!
    var giftPackageContentLabel:UILabel!
    var giftPackageNumLabel:UILabel!
    var giftPackageButton:UIButton!
    /// 优惠券
    var couponBg:UIView!
    var couponImageView:UIImageView!
    var couponTitleLabel:UILabel!
    var couponContentLabel:UILabel!
    var couponNumLabel:UILabel!
    var couponButton:UIButton!
    /// 折扣
    var onSaleLabel:UILabel!
    var grayView:UIView!
    
    var model:HWGameDetailModel? {
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            gameImageTopLabel.text = "TOP:\(model?.paihang ?? "")"
            nameLabel.text = model?.name
            sizeLabel.text = "大小：\(model?.size ?? "")M"
            typeLabel.text = model?.type
            score.text = "\(model?.point ?? 0.0)"
            starView.rating = model?.point ?? 0.0
            if (model?.zhekou.count ?? 0) > 0 {
                onSaleLabel.isHidden = false
                if model?.zhekou.hasSuffix("折") == true {
                    onSaleLabel.text = " \(model?.zhekou ?? "") "
                }else{
                    onSaleLabel.text = " \(model?.zhekou ?? "")折 "
                }
                if onSaleLabel.text == " 10折 " {
                    onSaleLabel.isHidden = true
                }
            }else {
                onSaleLabel.isHidden = true
            }
            if (model?.lb_num ?? -1) > 0 {
                giftPackageNumLabel.isHidden = false
                giftPackageNumLabel.text = "\(model!.lb_num)"
            }else {
                giftPackageNumLabel.isHidden = true
            }
            if (model?.coupon_num ?? -1) > 0 {
                couponNumLabel.isHidden = false
                couponNumLabel.text = "\(model!.coupon_num)"
            }else {
                couponNumLabel.isHidden = true
            }
            if (model?.small_coupon ?? "") == "" || (model?.max_coupon ?? "") == ""{
                couponContentLabel.text = "领取优惠券"
            }else {
                couponContentLabel.text = "领取" + "\(model?.small_coupon ?? "")" + "~" + "\(model?.max_coupon ?? "")" + "优惠券"
            }
            if model?.servers.code == "0" {
                openServiceInfo.text = model?.servers.message
            }else if model?.servers.code == "1" {
                openServiceInfo.text = (model?.servers.data[0].create_time ?? "") + "  " + (model?.servers.data[0].server_name ?? "")
            }else {
                 
            }
            
            
            if (model?.groupInfoModel.name.count ?? 0) > 0 {
                gameGroupBg.isHidden = false
                groupNameLabel.text = model?.groupInfoModel.name
                groupContentLabel.text = model?.groupInfoModel.new_xiaoxi

                let aStr = "已有\(model?.groupInfoModel.num ?? "")人加入"
                let attriString = NSMutableAttributedString.init(string: aStr)
                attriString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#FEAE09"), range: NSMakeRange(2, model?.groupInfoModel.num.count ?? 0))
                joinNumLabel.attributedText = attriString
                
                giftPackageBg.snp.remakeConstraints { make in
                    make.left.equalTo(15*widthScale)
                    make.top.equalTo(gameGroupBg.snp.bottom).offset(10*widthScale)
                    make.width.equalTo(168*widthScale)
                    make.height.equalTo(58*widthScale)
                }
            }else {
                gameGroupBg.isHidden = true
                giftPackageBg.snp.remakeConstraints { make in
                    make.left.equalTo(15*widthScale)
//                    make.top.equalTo(gameGroupBg.snp.bottom).offset(10*widthScale)
                    make.width.equalTo(168*widthScale)
                    make.height.equalTo(58*widthScale)
                    make.top.equalTo(openServiceImage.snp.bottom).offset(15*widthScale)
                }
            }
            
            textTagCollectionView.removeAllTags()
            for (idx,txt) in (model?.biaoqian ?? []).enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 12*widthScale)
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.cornerRadius = 2*widthScale
                normalStyle.extraSpace = CGSize.init(width: 8*widthScale, height: 4*widthScale)
                normalStyle.backgroundColor = .white
                normalStyle.shadowColor = .white
                if idx == 0 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FD9450")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#FE6A4C")
                }else if idx == 1 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#2B66FB")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#2B66FB")
                }else if idx == 2 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#7936FB")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#7936FB")
                }else if idx == 3 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#444444")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#444444")
                }else{
                    content.textColor = UIColor.colorWithHexString(hexString: "#CEA224")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#CEA224")
                }
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.style = normalStyle
                textTagCollectionView.addTag(tag)
            }
        }
    }
    
    var openServiceButtonClick:(()->())?
    var joinGroupButtonClick:((_:String)->())?
    var giftPackageButtonClick:(()->())?
    var couponButtonClick:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameDetailTopView {
    private func initUI() -> Void {
        gameImageView = UIImageView()
        gameImageView.backgroundColor = .clear
        gameImageView.layer.cornerRadius = 22*widthScale
        gameImageView.layer.masksToBounds = true
        gameImageView.contentMode = .scaleAspectFill
        addSubview(gameImageView)
        
        gameImageTopView = UIImageView()
        gameImageTopView.image = UIImage(named: "gameImageTop")
        gameImageView.addSubview(gameImageTopView)
        
        gameImageTopLabel = HWLabel.createBoldLabel(title: "-", color: "#FFFFFF", font: 12*widthScale)
        gameImageTopView.addSubview(gameImageTopLabel)
        
        nameLabel = HWLabel.createBoldLabel(title: "-", color: "#444444", font: 16*widthScale)
        nameLabel.numberOfLines = 1
        addSubview(nameLabel)
        
        onSaleLabel = HWLabel.createLabel(title: "", color: "FFFFFF", font: 10*widthScale)
        onSaleLabel.layer.cornerRadius = 3
        onSaleLabel.backgroundColor = .red
        onSaleLabel.layer.masksToBounds = true
        addSubview(onSaleLabel)
        
        sizeLabel = HWLabel.createLabel(title: "-", color: "#999999", font: 12*widthScale)
        addSubview(sizeLabel)
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#999999")
        addSubview(lineView)
        
        typeLabel = HWLabel.createLabel(title: "-", color: "#999999", font: 12*widthScale)
        addSubview(typeLabel)
        
        recommondLabel = HWLabel.createLabel(title: "推荐指数：", color: "#333333", font: 12*widthScale)
        addSubview(recommondLabel)
        
        starView = CosmosView.init()
        starView.rating = 0
        starView.settings.filledColor = .colorWithHexString(hexString: "#F97712")
        starView.settings.emptyColor = .clear
        starView.settings.starSize = 15*widthScale
        starView.settings.starMargin = 0
        starView.isUserInteractionEnabled = false
        starView.didFinishTouchingCosmos = {
            rating in
            
        }
        addSubview(starView)
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left
        textTagCollectionView.numberOfLines = 1
        textTagCollectionView.manualCalculateHeight = true;
        textTagCollectionView.showsHorizontalScrollIndicator = false
        textTagCollectionView.scrollDirection = .horizontal
        addSubview(textTagCollectionView)
        
        scoreBgView = UIImageView()
        scoreBgView.image = UIImage(named: "scoreBgImage")
        addSubview(scoreBgView)
        
        scoreLabel = HWLabel.createLabel(title: "评分", color: "#333333", font: 10*widthScale)
        scoreBgView.addSubview(scoreLabel)
        
        score = HWLabel.createBoldLabel(title: "", color: "#333333", font: 20*widthScale)
        scoreBgView.addSubview(score)
        
        openServiceImage = UIImageView()
        openServiceImage.image = UIImage(named: "openServiceImage")
        addSubview(openServiceImage)
        
        openServiceTitle = HWLabel.createBoldLabel(title: "开服动态：", color: "#333333", font: 12*widthScale)
        addSubview(openServiceTitle)
        
        openServiceInfo = HWLabel.createLabel(title: "", color: "#333333", font: 12*widthScale)
        addSubview(openServiceInfo)
        
        openServiceArrow = UIImageView()
        openServiceArrow.image = UIImage(named: "gameDetailRightArrow")
        addSubview(openServiceArrow)
        
        openServiceButton = UIButton(type: .custom)
        openServiceButton.addTarget(self, action: #selector(openServiceButtonClick(_:)), for: .touchUpInside)
        addSubview(openServiceButton)
        
        gameGroupBg = UIImageView()
        gameGroupBg.image = UIImage(named: "gameGroupBGView")
        gameGroupBg.isUserInteractionEnabled = true
        addSubview(gameGroupBg)
        
        groupNameLabel = HWLabel.createBoldLabel(title: "", color: "#444444", font: 14*widthScale)
        gameGroupBg.addSubview(groupNameLabel)
        
        groupContentLabel = HWLabel.createLabel(title: "", color: "#666666", font: 10*widthScale)
        gameGroupBg.addSubview(groupContentLabel)
        
        joinGroupLabel = HWLabel.createBoldLabel(title: "加入群聊>", color: "#444444", font: 14*widthScale)
        gameGroupBg.addSubview(joinGroupLabel)
        
        joinNumLabel = HWLabel.createLabel(title: "", color: "#666666", font: 10*widthScale)
        gameGroupBg.addSubview(joinNumLabel)
        
        joinGroupButton = UIButton(type: .custom)
        joinGroupButton.addTarget(self, action: #selector(joinGroupButtonClick(_:)), for: .touchUpInside)
        gameGroupBg.addSubview(joinGroupButton)
        
        giftPackageBg = UIView()
        giftPackageBg.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        giftPackageBg.layer.cornerRadius = 6*widthScale
        addSubview(giftPackageBg)
        
        giftPackageImageView = UIImageView()
        giftPackageImageView.image = UIImage(named: "giftPackageImage")
        giftPackageBg.addSubview(giftPackageImageView)
        
        giftPackageTitleLabel = HWLabel.createBoldLabel(title: "礼包", color: "#333333", font: 14*widthScale)
        giftPackageBg.addSubview(giftPackageTitleLabel)
        
        giftPackageContentLabel = HWLabel.createLabel(title: "专属游戏大礼包", color: "#999999", font: 10*widthScale)
        giftPackageBg.addSubview(giftPackageContentLabel)
        
        giftPackageNumLabel = HWLabel.createLabel(title: "", color: "#FFFFFF", font: 10*widthScale)
        giftPackageNumLabel.textAlignment = .center
        giftPackageNumLabel.backgroundColor = .colorWithHexString(hexString: "#FF5A4F")
        giftPackageNumLabel.layer.cornerRadius = 8*widthScale
        giftPackageNumLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FFFFFF").cgColor
        giftPackageNumLabel.layer.borderWidth = 1*widthScale
        giftPackageNumLabel.layer.masksToBounds = true
        giftPackageBg.addSubview(giftPackageNumLabel)
        
        giftPackageButton = UIButton(type: .custom)
        giftPackageButton.addTarget(self, action: #selector(giftPackageButtonClick(_:)), for: .touchUpInside)
        giftPackageBg.addSubview(giftPackageButton)
        
        couponBg = UIView()
        couponBg.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        couponBg.layer.cornerRadius = 6*widthScale
        addSubview(couponBg)
        
        couponImageView = UIImageView()
        couponImageView.image = UIImage(named: "couponImage")
        couponBg.addSubview(couponImageView)
        
        couponTitleLabel = HWLabel.createBoldLabel(title: "优惠券", color: "#333333", font: 14*widthScale)
        couponBg.addSubview(couponTitleLabel)
        
        couponContentLabel = HWLabel.createLabel(title: "", color: "#999999", font: 10*widthScale)
        couponBg.addSubview(couponContentLabel)
        
        couponNumLabel = HWLabel.createLabel(title: "", color: "#FFFFFF", font: 10*widthScale)
        couponNumLabel.textAlignment = .center
        couponNumLabel.backgroundColor = .colorWithHexString(hexString: "#FF5A4F")
        couponNumLabel.layer.cornerRadius = 8*widthScale
        couponNumLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FFFFFF").cgColor
        couponNumLabel.layer.borderWidth = 1*widthScale
        couponNumLabel.layer.masksToBounds = true
        couponBg.addSubview(couponNumLabel)
        
        couponButton = UIButton(type: .custom)
        couponButton.addTarget(self, action: #selector(couponButtonClick(_:)), for: .touchUpInside)
        couponBg.addSubview(couponButton)
        
        giftPackageNumLabel.isHidden = true
        couponNumLabel.isHidden = true
        
        grayView = UIView()
        grayView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        addSubview(grayView)
    }
    
    private func initFrame() -> Void {
        gameImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(96*widthScale)
        }
        
        gameImageTopView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(4*widthScale)
            make.height.equalTo(16*widthScale)
        }
        
        gameImageTopLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(gameImageView)
        }
        
        onSaleLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(10*widthScale)
            make.right.lessThanOrEqualTo(-10*widthScale)
            make.height.equalTo(13*widthScale)
            make.centerY.equalTo(nameLabel)
        }
        
        sizeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(6*widthScale)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(sizeLabel.snp.right).offset(10*widthScale)
            make.width.equalTo(1*widthScale)
            make.height.equalTo(11*widthScale)
            make.centerY.equalTo(sizeLabel)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(lineView.snp.right).offset(10*widthScale)
            make.centerY.equalTo(sizeLabel)
        }
        
        recommondLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(sizeLabel.snp.bottom).offset(10*widthScale)
        }
        
        starView.snp.makeConstraints { make in
            make.left.equalTo(recommondLabel.snp.right).offset(4*widthScale)
            make.centerY.equalTo(recommondLabel)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(-15*widthScale)
            make.bottom.equalTo(gameImageView)
        }
        
        scoreBgView.snp.makeConstraints { make in
            make.width.height.equalTo(59*widthScale)
            make.centerY.equalTo(gameImageView)
            make.right.equalTo(-15*widthScale)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(12*widthScale)
        }
        
        score.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-11*widthScale)
        }
        
        openServiceImage.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.width.height.equalTo(16*widthScale)
            make.top.equalTo(gameImageView.snp.bottom).offset(16*widthScale)
        }
        
        openServiceTitle.snp.makeConstraints { make in
            make.centerY.equalTo(openServiceImage)
            make.left.equalTo(openServiceImage.snp.right).offset(10*widthScale)
        }
        
        openServiceInfo.snp.makeConstraints { make in
            make.left.equalTo(openServiceTitle.snp.right)
            make.right.equalTo(openServiceArrow.snp.left).offset(-10*widthScale)
            make.centerY.equalTo(openServiceTitle)
        }
        
        openServiceArrow.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.width.equalTo(7*widthScale)
            make.height.equalTo(10*widthScale)
            make.centerY.equalTo(openServiceImage)
        }
        
        openServiceButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(openServiceTitle)
            make.right.equalTo(openServiceInfo.snp.right)
        }
        
        openServiceTitle.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        gameGroupBg.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(openServiceImage.snp.bottom).offset(20*widthScale)
            make.height.equalTo(53*widthScale)
        }
        
        groupNameLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(10*widthScale)
//            make.right.equalTo(joinGroupLabel.snp.left).offset(-10*widthScale)
        }
        
        groupContentLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(groupNameLabel.snp.bottom).offset(6*widthScale)
        }
        
        joinGroupLabel.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(10*widthScale)
        }
        
        joinNumLabel.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(joinGroupLabel.snp.bottom).offset(6*widthScale)
        }
        
        joinGroupButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        giftPackageBg.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(gameGroupBg.snp.bottom).offset(10*widthScale)
            make.width.equalTo(168*widthScale)
            make.height.equalTo(58*widthScale)
        }
        
        giftPackageImageView.snp.makeConstraints { make in
            make.left.equalTo(14*widthScale)
            make.top.equalTo(17*widthScale)
            make.width.equalTo(23*widthScale)
            make.height.equalTo(26*widthScale)
        }
        
        giftPackageTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(giftPackageImageView.snp.right).offset(11*widthScale)
            make.top.equalTo(11*widthScale)
        }
        
        giftPackageContentLabel.snp.makeConstraints { make in
            make.left.equalTo(giftPackageTitleLabel)
            make.bottom.equalTo(-12*widthScale)
        }
        
        giftPackageNumLabel.snp.makeConstraints { make in
            make.right.equalTo(-6*widthScale)
            make.width.height.equalTo(16*widthScale)
            make.top.equalTo(3*widthScale)
        }
        
        giftPackageButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        couponBg.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(giftPackageBg)
            make.width.equalTo(168*widthScale)
            make.height.equalTo(58*widthScale)
        }
        
        couponImageView.snp.makeConstraints { make in
            make.left.equalTo(14*widthScale)
            make.top.equalTo(17*widthScale)
            make.width.equalTo(23*widthScale)
            make.height.equalTo(26*widthScale)
        }
        
        couponTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(couponImageView.snp.right).offset(11*widthScale)
            make.top.equalTo(11*widthScale)
        }
        
        couponContentLabel.snp.makeConstraints { make in
            make.left.equalTo(couponTitleLabel)
            make.bottom.equalTo(-12*widthScale)
        }
        
        couponNumLabel.snp.makeConstraints { make in
            make.right.equalTo(-6*widthScale)
            make.width.height.equalTo(16*widthScale)
            make.top.equalTo(3*widthScale)
        }
        
        couponButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        grayView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(10*widthScale)
        }
        
        onSaleLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
}

extension HWGameDetailTopView {
    @objc func openServiceButtonClick(_:UIButton) -> Void {
        self.openServiceButtonClick?()
    }
    
    @objc func joinGroupButtonClick(_:UIButton) -> Void {
        self.joinGroupButtonClick?(self.model?.groupInfoModel.url ?? "")
    }
    
    @objc func giftPackageButtonClick(_:UIButton) -> Void {
        self.giftPackageButtonClick?()
    }
    
    @objc func couponButtonClick(_:UIButton) -> Void {
        self.couponButtonClick?()
    }
    
}
